document.addEventListener('DOMContentLoaded', () => {
    // 获取DOM元素
    const uploadArea = document.getElementById('upload-area');
    const fileInput = document.getElementById('file-input');
    const fileInfo = document.getElementById('file-info');
    const selectedFileName = document.getElementById('selected-file-name');
    const convertButton = document.getElementById('convert-button');
    const resultCard = document.getElementById('result-card');
    const resultFileName = document.getElementById('result-file-name');
    const downloadLink = document.getElementById('download-link');
    const newConvertButton = document.getElementById('new-convert-button');
    const loadingOverlay = document.getElementById('loading-overlay');
    const errorToast = document.getElementById('error-toast');
    const errorMessage = document.getElementById('error-message');
    
    // 设置当前时间
    const updateTime = () => {
        const now = new Date();
        const hours = now.getHours().toString().padStart(2, '0');
        const minutes = now.getMinutes().toString().padStart(2, '0');
        document.querySelector('.status-bar-time').textContent = `${hours}:${minutes}`;
    };
    
    // 每分钟更新一次时间
    updateTime();
    setInterval(updateTime, 60000);
    
    // 点击上传区域触发文件选择
    uploadArea.addEventListener('click', () => {
        fileInput.click();
    });
    
    // 处理拖放文件
    uploadArea.addEventListener('dragover', (e) => {
        e.preventDefault();
        uploadArea.classList.add('dragover');
    });
    
    uploadArea.addEventListener('dragleave', () => {
        uploadArea.classList.remove('dragover');
    });
    
    uploadArea.addEventListener('drop', (e) => {
        e.preventDefault();
        uploadArea.classList.remove('dragover');
        
        if (e.dataTransfer.files.length > 0) {
            handleFileSelection(e.dataTransfer.files[0]);
        }
    });
    
    // 处理文件选择
    fileInput.addEventListener('change', () => {
        if (fileInput.files.length > 0) {
            handleFileSelection(fileInput.files[0]);
        }
    });
    
    // 文件选择处理函数
    function handleFileSelection(file) {
        const allowedExtensions = ['.doc', '.docx', '.xls', '.xlsx', '.ppt', '.pptx'];
        const fileExtension = '.' + file.name.split('.').pop().toLowerCase();
        
        if (!allowedExtensions.includes(fileExtension)) {
            showError('不支持的文件类型，请选择Office文档');
            return;
        }
        
        // 显示所选文件信息
        selectedFileName.textContent = file.name;
        uploadArea.classList.add('hidden');
        fileInfo.classList.remove('hidden');
    }
    
    // 点击转换按钮
    convertButton.addEventListener('click', () => {
        if (fileInput.files.length === 0) {
            showError('请先选择一个文件');
            return;
        }
        
        // 创建FormData对象
        const formData = new FormData();
        formData.append('file', fileInput.files[0]);
        
        // 显示加载动画
        loadingOverlay.classList.remove('hidden');
        
        // 发送转换请求
        fetch('/upload', {
            method: 'POST',
            body: formData
        })
        .then(response => response.json())
        .then(data => {
            // 隐藏加载动画
            loadingOverlay.classList.add('hidden');
            
            if (data.success) {
                // 转换成功
                resultFileName.textContent = data.filename;
                downloadLink.setAttribute('href', data.download_url);
                fileInfo.classList.add('hidden');
                resultCard.classList.remove('hidden');
            } else {
                // 转换失败
                showError(data.error || '转换失败，请重试');
            }
        })
        .catch(error => {
            loadingOverlay.classList.add('hidden');
            showError('网络错误，请重试');
            console.error('Error:', error);
        });
    });
    
    // 开始新转换
    newConvertButton.addEventListener('click', () => {
        // 重置界面
        fileInput.value = '';
        resultCard.classList.add('hidden');
        uploadArea.classList.remove('hidden');
    });
    
    // 显示错误提示
    function showError(message) {
        errorMessage.textContent = message;
        errorToast.classList.remove('hidden');
        
        // 3秒后自动隐藏错误提示
        setTimeout(() => {
            errorToast.classList.add('hidden');
        }, 3000);
    }
}); 